import 'package:flutter/material.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

/// [BluetoothStatusIcon] 是一个自定义的蓝牙连接状态图标组件
class BluetoothStatusIcon extends StatefulWidget {
  final String imgPath;
  final double size;
  final Color connectedColor;
  final Color disconnectedColor;
  final Color connectingColor;
  final Color connectingbackgroundColor;
  final BluetoothConnectionState state;
  final double animationScale;

  BluetoothStatusIcon(
    this.imgPath, {
    Key? key,
    this.size = 50.0,
    Color? connectedColor,
    this.disconnectedColor = GrayColor.gray4,
    Color? connectingbackgroundColor,
    Color? connectingColor,
    this.state = BluetoothConnectionState.disconnected,
    this.animationScale = 1.5,
  })  : connectedColor = LxWidgetsLibraryService.to.colorType.color6,
        connectingbackgroundColor = LxWidgetsLibraryService.to.colorType.color6,
        connectingColor = connectingColor ??
            LxWidgetsLibraryService.to.colorType.color6.withOpacity(0.4),
        super(key: key);

  @override
  _BluetoothStatusIconState createState() => _BluetoothStatusIconState();
}

enum BluetoothConnectionState {
  disconnected, // 未连接
  connecting, // 连接中
  connected, // 已连接
}

class _BluetoothStatusIconState extends State<BluetoothStatusIcon>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _scaleAnimation;
  late Animation<double> _opacityAnimation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 400),
      vsync: this,
    );

    if (widget.state == BluetoothConnectionState.connecting) {
      _controller.repeat(reverse: true);
    }

    _initAnimations();
  }

  void _initAnimations() {
    _scaleAnimation = Tween(
      begin: 1.0,
      end: widget.animationScale,
    ).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.easeInOut,
      ),
    );

    _opacityAnimation = Tween(
      begin: 0.3,
      end: 0.0,
    ).animate(
      CurvedAnimation(
        parent: _controller,
        curve: Curves.easeOut,
      ),
    );
  }

  @override
  void didUpdateWidget(BluetoothStatusIcon oldWidget) {
    super.didUpdateWidget(oldWidget);

    // 状态变化时处理动画
    if (oldWidget.state != widget.state) {
      if (widget.state == BluetoothConnectionState.connecting) {
        _controller.repeat(reverse: true);
      } else {
        _controller.stop();
      }
    }

    if (oldWidget.animationScale != widget.animationScale) {
      _initAnimations();
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Color _getColor() {
    switch (widget.state) {
      case BluetoothConnectionState.connected:
        return widget.connectedColor;
      case BluetoothConnectionState.connecting:
        return widget.connectingColor;
      case BluetoothConnectionState.disconnected:
        return widget.disconnectedColor;
    }
  }

  @override
  Widget build(BuildContext context) {
    final color = _getColor();

    switch (widget.state) {
      case BluetoothConnectionState.connected:
        return Image.asset(widget.imgPath,
                width: widget.size, height: widget.size, fit: BoxFit.fill)
            .colorFiltered(
          color: color,
        );
      case BluetoothConnectionState.disconnected:
        return Image.asset(widget.imgPath,
                width: widget.size, height: widget.size, fit: BoxFit.fill)
            .colorFiltered(
          color: color,
        );
      case BluetoothConnectionState.connecting:
        return SizedBox(
          width: widget.size * widget.animationScale, // 固定容器大小
          height: widget.size * widget.animationScale,
          child: AnimatedBuilder(
            animation: _controller,
            builder: (context, child) {
              return Stack(
                alignment: Alignment.center,
                children: [
                  // 外层光环
                  Opacity(
                    opacity: _opacityAnimation.value,
                    child: Container(
                      width: widget.size * _scaleAnimation.value * 1.5,
                      height: widget.size * _scaleAnimation.value * 1.5,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: widget.connectingbackgroundColor,
                      ),
                    ),
                  ),
                  // 缩放的主图标
                  Transform.scale(
                    scale: _scaleAnimation.value,
                    child: Image.asset(widget.imgPath,
                            width: widget.size,
                            height: widget.size,
                            fit: BoxFit.fill)
                        .colorFiltered(
                      color: color,
                    ),
                  ),
                  // 中心固定大小的图标
                  Image.asset(
                    widget.imgPath,
                    width: widget.size * 0.8,
                    height: widget.size * 0.8,
                    fit: BoxFit.fill,
                  ).colorFiltered(
                    color: color.withOpacity(0.8),
                  ),
                ],
              );
            },
          ),
        );
    }
  }
}
